package com.augurit.gzcgwsp.csdlwj.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.dozer.DozerBeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.augurit.common.util.HqlUtils;
import com.augurit.common.util.PageUtils;
import com.augurit.common.util.lang.StringUtils;
import com.augurit.gzcgwsp.csdlwj.convert.YwComQyxxCsdlwjConvertor;
import com.augurit.gzcgwsp.csdlwj.dao.YwComQyxxCsdlwjDao;
import com.augurit.gzcgwsp.csdlwj.entity.YwComQyxxCsdlwj;
import com.augurit.gzcgwsp.csdlwj.service.IYwComQyxxCsdlwjService;
import com.augurit.gzcgwsp.csdlwj.web.form.YwComQyxxCsdlwjForm;

@Service
@Transactional
public class YwComQyxxCsdlwjServiceImpl implements IYwComQyxxCsdlwjService{

	@Autowired
	private YwComQyxxCsdlwjDao ywComQyxxCsdlwjDao;
	
	@Transactional(readOnly = true)
	public YwComQyxxCsdlwjForm get(Long id) {
		YwComQyxxCsdlwj entity = ywComQyxxCsdlwjDao.get(id);
		return YwComQyxxCsdlwjConvertor.convertVoToForm(entity);
	}
	
	@Transactional(readOnly = true)
    public YwComQyxxCsdlwjForm getFormByJsdw(String jsdw) {
		YwComQyxxCsdlwj entity = ywComQyxxCsdlwjDao.findUnique("from YwComQyxxCsdlwj t where t.jsdw = '" + jsdw + "'");
        if (entity == null) return null;
        return YwComQyxxCsdlwjConvertor.convertVoToForm(entity);
    }
	
	public void delete(Long... ids) {
		ywComQyxxCsdlwjDao.delete(ids);
	}

	public void save(YwComQyxxCsdlwjForm... forms) {
		if(forms != null)
			for(YwComQyxxCsdlwjForm form : forms)
				this.save(form);
	}
	
	public void update(YwComQyxxCsdlwjForm form) {
	    YwComQyxxCsdlwjForm dbform = null;
	    if (form.getId() != null && form.getId() != 0) dbform = this.get(form.getId());
	    
	    if (dbform == null) {
	        this.save(form);
	    } else {
	        new DozerBeanMapper().map(form, dbform);
            this.save(dbform);
	    }
	}
	
	public void save(YwComQyxxCsdlwjForm form){
		
		if(form != null){
			YwComQyxxCsdlwj entity = null;
			
			if(form != null && form.getId() != null){
				entity = ywComQyxxCsdlwjDao.get(form.getId());
				if(entity.getId()==null){
					entity = new YwComQyxxCsdlwj();
					entity.setId(form.getId());
				}
			}else{
				entity = new YwComQyxxCsdlwj();
			}
			
			YwComQyxxCsdlwjConvertor.convertFormToVo(form, entity);
			
			ywComQyxxCsdlwjDao.save(entity);
			
			form.setId(entity.getId());
		}
	}

	@Transactional(readOnly = true)
	public Page<YwComQyxxCsdlwjForm> search(Page<YwComQyxxCsdlwjForm> page, YwComQyxxCsdlwjForm form) {
		Page pg = PageUtils.newPageInstance(page);
		
		StringBuffer hql = new StringBuffer("from YwComQyxxCsdlwj ps where 1=1");
		List values = new ArrayList();
		
		if(form != null){
		    if (StringUtils.isNotBlank(form.getJsdw())) {
		        hql.append(" and ps.jsdw like '%" + form.getJsdw() + "%'");
		    }
		}
		
		hql.append(HqlUtils.buildOrderBy(page, "ps"));
		System.out.println("hql ============== "+hql.toString());
		ywComQyxxCsdlwjDao.findPage(pg, hql.toString(), values);
		System.out.println("11 ============== ");
		List<YwComQyxxCsdlwjForm> list = YwComQyxxCsdlwjConvertor.convertVoListToFormList(pg.getResult());
		System.out.println("22 ============== ");
		PageUtils.copy(pg, list, page);
		System.out.println("page ============== "+page.getResult().size());
		return page;
	}
	
	@Transactional(readOnly = true)
	public Page<YwComQyxxCsdlwjForm> search(Page<YwComQyxxCsdlwjForm> page, List<PropertyFilter> filters) {
		Page<YwComQyxxCsdlwj> pg = PageUtils.newPageInstance(page);
		
		ywComQyxxCsdlwjDao.findPage(pg, filters);
		
		List<YwComQyxxCsdlwjForm> list = YwComQyxxCsdlwjConvertor.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}

	public void updateYwComQyxxCsdlwj(YwComQyxxCsdlwjForm form) {
	    YwComQyxxCsdlwjForm dbform = null;
	    if (form.getId() != null && form.getId() != 0)
	    	dbform = this.get(form.getId());
	    if (dbform != null) {
	    	YwComQyxxCsdlwj entity = null;
			entity = ywComQyxxCsdlwjDao.getYwComQyxxCsdlwj(dbform.getId());
			YwComQyxxCsdlwjConvertor.convertFormToVo(form, entity);
			if(entity!=null){
				ywComQyxxCsdlwjDao.updateYwComQyxxCsdlwj(entity);
			}
	    }
	}

	/*
	 * (non-Javadoc)
	 * @see com.augurit.gzcgwsp.ywcmn.service.IYwComQyxxService#getYwComQyxx(java.lang.Long, java.lang.String)
	 */
    public YwComQyxxCsdlwj getYwComQyxxCsdlwj(Long id, String acceptNo) {
        return ywComQyxxCsdlwjDao.getYwComQyxxCsdlwj(id);
    }
}
